package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * http://www.duowan.com/ 多玩游戏网
 *  abc
 */
public class DuowanNewsAnalyse implements AnalyseNews{
	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://[a-z]*.duowan.com/[0-9]*/[0-9]*.html"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}

		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}

		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		if(doc.select("meta").size()==1){
			String attr=doc.select("meta").attr("content");
			if(StringUtils.isNotBlank(attr)){
				String referUrl=StringUtils.substringAfter(attr, "URL=");
				UrlMeta responseToURL=CrawlHTML.responseToURL(referUrl);
				if(responseToURL!=null){
					String html=responseToURL.getHtml();
					if(StringUtils.isNotBlank(html)){
						doc=Jsoup.parse(html);
					}
				}
			}
		}
		news.setUrl(url);

		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		String dateStr=null;
		title =doc.select("title").text();
		title=StringUtils.substringBefore(title, " -  - ");
		title=StringUtils.substringBefore(title, "_");
		title=StringUtils.substringBefore(title, "-");
		dateStr=doc.select("address").text();

		Elements select=doc.select("#text");
		boolean remove=false;
		for (Element element : select) {
			Elements children=element.children();
			for (int i=0; i < children.size(); i++) {
				if(remove || children.get(i) .nodeName().equals("table")){
					remove=true;
					children.get(i).remove();
				}
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("#text"));
		date=JSoupUtils.matchDate(doc,"发表时间");
		if(date==null){
			date=DateUtils.matchDate(doc.select("address").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("p#articleinfo").text());
			}
		}
		author=StringUtils.substringBetween(dateStr, "来源：", "|");
		if(StringUtils.isBlank(author)){
			author=StringUtils.substringBetween(dateStr, "来源：", "作者：");
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		DuowanNewsAnalyse a=new DuowanNewsAnalyse();
		String url="http://newgame.duowan.com/1402/256263677427.html";
		String fromatUrl=UrlArgumentTop.FromatUrl(url);
		System.out.println("url="+fromatUrl);
		System.out.println(a.isDetailPage(fromatUrl));
		UrlMeta meta=CrawlHTML.responseToURL(fromatUrl);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
